<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class CoursesUsersLikeModel extends Model {

    protected $table ="courses_users_like";

    protected $pk = 'courses_users_like_id';

    private $cacheKey="courses:users:like:id:";

    private $cacheTime=5184000;


    /*
     * 获取total
     * */
    public function getTotal($where=[]){
        return Db::name($this->table)->where($where)->count();
    }


    public function isLike($course_review_id,$uid){

        $cacheKey = $this->cacheKey.$uid.":".$course_review_id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info == 1){
           return 1;
        }else{
           $where=[];
           $where[] = ['course_review_id','=',$course_review_id];
           $where[] = ['uid','=',$uid];
           $isLike = $this->getTotal($where);
           if($isLike){
               $redisModel->set($cacheKey,1,$this->cacheTime);
               return 1;
           }else{
               $redisModel->set($cacheKey,-1,$this->cacheTime);
               return 0;
           }
        }
    }



    public function cacelLike($course_review_id,$uid){
        $cacheKey = $this->cacheKey.$uid.":".$course_review_id;
        $redisModel = new RedisModel();
       return $redisModel->set($cacheKey,-1,$this->cacheTime);
    }

    public function doLike($course_review_id,$uid){
        $cacheKey = $this->cacheKey.$uid.":".$course_review_id;
        $redisModel = new RedisModel();
        return $redisModel->set($cacheKey,1,$this->cacheTime);
    }
}